<template>
    <div>
        <breadcrumb :title="'导航管理'" :noBack="true"></breadcrumb>
        <div class="white-block box" v-loading="loading">
            <div class="text-right">
                <el-button type="primary" size="small" @click="$router.push($route.path+'/add')" :readOnly="false">
                    新增导航
                </el-button>
            </div>
            <table-example :data="tableData" :tableColumn="header" :slotColumns="slotColumns" class="mt20" ref="table">
                <template slot="operation" slot-scope="scope">
                    <el-link type="primary" :underline="false" class="mr10"
                             @click="$router.push($route.path+'/'+scope.row.id)">编辑
                    </el-link>
                    <el-link type="primary" :underline="false" class="mr10" @click="removeList(scope.row.id)">删除
                    </el-link>
                    <el-link type="primary" :underline="false"
                             :class="{mr10:tableData.length>1&&scope.row.sort<tableData.length}"
                             v-if="tableData.length>1&&scope.row.sort>1"
                             @click="rowUpDown(scope.row,0)">
                        向上
                    </el-link>
                    <el-link type="primary" :underline="false" @click="rowUpDown(scope.row,1)"
                             v-if="tableData.length>1&&scope.row.sort<tableData.length">向下
                    </el-link>
                </template>
                <template slot="displayStatus" slot-scope="scope">
                    <span v-if="scope.row.displayStatus==0">显示</span>
                    <span v-else class="error">隐藏</span>
                </template>
                <template slot="openType" slot-scope="scope">
                    <span v-if="scope.row.openType==1">新页面打开</span>
                    <span v-else>当前页面打开</span>
                </template>
                <template slot="navigationPath" slot-scope="scope">
                    <span
                        v-if="!isNaN(Number(scope.row.navigationPath))">{{'/list?id='+scope.row.navigationPath}}</span>
                    <span v-else>{{scope.row.navigationPath}}</span>
                </template>
            </table-example>
        </div>
        <transition name="slide">
            <router-view @upload="getList"></router-view>
        </transition>
    </div>
</template>

<script>
    import breadcrumb from '../../components/breadcrumb'
    import tableExample from '../../components/table'
    import page from '../../components/pagination'

    export default {

        data() {
            return {
                tableData: [],
                header: [
                    {
                        prop: 'sort',
                        label: '编号',
                        minWidth: '17%',
                        align: 'center'
                    },
                    {
                        prop: 'navigationName',
                        label: '导航名称',
                        minWidth: '17%',
                        align: 'center'
                    },
                    {
                        prop: 'navigationPath',
                        label: '路径',
                        minWidth: '17%',
                        align: 'center'
                    },
                    {
                        prop: 'openType',
                        label: '打开方式',
                        minWidth: '17%',
                        align: 'center'
                    },
                    {
                        prop: 'displayStatus',
                        label: '当前状态',
                        minWidth: '15%',
                        align: 'center'
                    },
                    {
                        prop: 'operation',
                        label: '操作',
                        minWidth: '17%',
                        align: 'center'
                    }
                ],
                slotColumns: ['operation', 'displayStatus', 'openType', 'navigationPath'],
                data: null,
                input: '',
                value: '',
                limit: 5,
                loading: false,
                index: 1
            }
        },
        mounted() {
            this.getList()
        },
        methods: {
            getLength(row) {
                let i = null
                this.tableData.forEach((list, index) => {
                    if (list === row) {
                        i = index
                    }
                })
                return i
            },
            rowUp(row, index) {
                this.tableData.splice(index, 1)
                row.code--
                this.tableData[index - 1].code++
                this.tableData.splice(index - 1, 0, row)
            },
            rowDown(row, index) {
                row.code++
                this.tableData[index + 1].code--
                this.tableData.splice(index + 2, 0, row)
                this.tableData.splice(index, 1)
            },
            rowUpDown(row, index) {
                this.loading = true
                this.$http.post('/gsNavigation/levelUpOrDown', {
                    id: row.id,
                    type: index
                }).then(result => {
                    if (result.success) {
                        this.getList()
                    } else {
                        this.loading = false
                    }
                }).catch(() => {
                    this.loading = false
                })
            },
            getList(index) {
                this.loading = true
                if (index) this.index = index
                this.$http.get('/gsNavigation/getNavigationList', {
                    current: this.index || 1,
                    size: 10
                }).then(result => {
                    this.loading = false
                    if (result.success) {
                        this.tableData = result.data
                    }
                    this.$method.updateView()
                }).catch(() => {
                    this.loading = false
                })
            },
            removeList(id) {
                this.loading = true
                this.$http.post('/gsNavigation/deleteById', {
                    id
                }).then(result => {
                    if (result.success) {
                        this.getList()
                    } else {
                        this.loading = false
                    }
                }).catch(() => {
                    this.loading = false
                })
            }
        },
        components: {
            tableExample,
            breadcrumb,
            page
        }
    }
</script>

<style scoped lang="less">
    .box {
        .input {
            width: 2.8rem;
            height: 0.5rem;
            line-height: 0.5rem;
        }
        .btn {
            border-radius: 0;
        }
        .btn-s {
            padding: 0 0.08rem;
        }
        .button {
            border: none;
            background-color: #fff;
            color: #1497fc;
        }
        .page {
            margin-top: 1.11rem;
        }
    }
</style>
